using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using EasyCommerce.BLL;
using EasyCommerce.DAL;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_SiteUserEdit : BasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        SiteUser siteUser;

        if (Int32.TryParse(Request["id"], out id))
            siteUser = SiteUser.Load(id);
        else
            siteUser = null;

        if (siteUser != null)
        {
            if (!string.IsNullOrEmpty(siteUser.UserName))
				tbxUserName.Text = siteUser.UserName;
			if (!string.IsNullOrEmpty(siteUser.UserPassword))
				tbxUserPassword.Text = siteUser.UserPassword;
			if (!string.IsNullOrEmpty(siteUser.EmailAddress))
				tbxEmailAddress.Text = siteUser.EmailAddress;
			cbxIsActive.Checked = siteUser.IsActive;
			cbxIsOnline.Checked = siteUser.IsOnline;
			cbxIsConfirmed.Checked = siteUser.IsConfirmed;
			if (!string.IsNullOrEmpty(siteUser.ConfirmationCode))
				tbxConfirmationCode.Text = siteUser.ConfirmationCode;
			if (siteUser.RegisterDate != SqlDateTime.MinValue)
				tbxRegisterDate.Text = siteUser.RegisterDate.ToShortDateString();
			if (siteUser.LastLogin != SqlDateTime.MinValue)
				tbxLastLogin.Text = siteUser.LastLogin.ToShortDateString();
			if (!string.IsNullOrEmpty(siteUser.SecurityQuestion))
				tbxSecurityQuestion.Text = siteUser.SecurityQuestion;
			if (!string.IsNullOrEmpty(siteUser.SecurityAnswer))
				tbxSecurityAnswer.Text = siteUser.SecurityAnswer;
			tbxVisitCount.Text = siteUser.VisitCount.ToString();
			if (!string.IsNullOrEmpty(siteUser.RegisterIp))
				tbxRegisterIp.Text = siteUser.RegisterIp;
			tbxFailedLoginCount.Text = siteUser.FailedLoginCount.ToString();
			tbxUserRoleId.Text = siteUser.UserRoleId.ToString();

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string userName = Security.BindStringInput(tbxUserName.Text, _securityLevel);
		string userPassword = Security.BindStringInput(tbxUserPassword.Text, _securityLevel);
		string emailAddress = Security.BindStringInput(tbxEmailAddress.Text, _securityLevel);
		bool isActive = cbxIsActive.Checked;
		bool isOnline = cbxIsOnline.Checked;
		bool isConfirmed = cbxIsConfirmed.Checked;
		string confirmationCode = Security.BindStringInput(tbxConfirmationCode.Text, _securityLevel);
		DateTime registerDate = Security.BindDateTimeInput(tbxRegisterDate.Text, _securityLevel);
		DateTime lastLogin = Security.BindDateTimeInput(tbxLastLogin.Text, _securityLevel);
		string securityQuestion = Security.BindStringInput(tbxSecurityQuestion.Text, _securityLevel);
		string securityAnswer = Security.BindStringInput(tbxSecurityAnswer.Text, _securityLevel);
		int visitCount = Security.BindIntegerInput(tbxVisitCount.Text, _securityLevel);
		string registerIp = Security.BindStringInput(tbxRegisterIp.Text, _securityLevel);
		int failedLoginCount = Security.BindIntegerInput(tbxFailedLoginCount.Text, _securityLevel);
		int userRoleId = Security.BindIntegerInput(tbxUserRoleId.Text, _securityLevel);

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (SiteUser.Save(
				id, 
				userName,
				userPassword,
				emailAddress,
				isActive,
				isOnline,
				isConfirmed,
				confirmationCode,
				registerDate,
				lastLogin,
				securityQuestion,
				securityAnswer,
				visitCount,
				registerIp,
				failedLoginCount,
				userRoleId
                ))
                    Response.Redirect("siteuserlist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            SiteUser siteUser = SiteUser.Create(
				userName,
				userPassword,
				emailAddress,
				isActive,
				isOnline,
				isConfirmed,
				confirmationCode,
				registerDate,
				lastLogin,
				securityQuestion,
				securityAnswer,
				visitCount,
				registerIp,
				failedLoginCount,
				userRoleId
                );

            if (siteUser != null)
                Response.Redirect("siteuserlist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("siteuserlist.aspx");
    }
}
